package com.thermal.thermal_inspection.controller;

import com.thermal.thermal_inspection.common.CommonResponse;
import com.thermal.thermal_inspection.entity.Inspection;
import com.thermal.thermal_inspection.entity.InspectionGroup;
import com.thermal.thermal_inspection.service.InspectionGroupService;
import com.thermal.thermal_inspection.service.InspectionService;
import com.thermal.thermal_inspection.vo.InspectionSearchVO;
import com.thermal.thermal_inspection.vo.InspectionVO;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/inspection")
public class InspectionSearchController {
    private static final Logger logger = Logger.getLogger(InspectionSearchController.class);

    @Autowired
    InspectionService inspectionService;
    @Autowired
    InspectionGroupService inspectionGroupService;

    /**
     *通过时间，用户id，地图id查询inspectionVOList
     */
    @PostMapping("/search/{userId}")
    public CommonResponse<List<InspectionVO>> searchInspection(@PathVariable int userId, @RequestBody InspectionSearchVO inspectionSearchVO){
        Timestamp startTime = inspectionSearchVO.getStartTime();
        Timestamp endTime = inspectionSearchVO.getEndTime();
        int searchUserId = inspectionSearchVO.getSearchUserId();
        int mapId = inspectionSearchVO.getMapId();

        List<InspectionVO> inspectionVOList = inspectionService.searchInspection(userId, startTime, endTime, searchUserId, mapId);
        if(inspectionVOList.isEmpty()){
            logger.error("任务搜索失败");
            return CommonResponse.createForError("任务搜索失败");
        }
        logger.info("任务搜索成功");
        return CommonResponse.createForSuccess("SUCCESS", inspectionVOList);
    }

    /**
     * 根据创建者or模板名称or负责班组搜索模板，响应结构与“查看所有任务模板接口一致”
     */
    @GetMapping("/search")
    public CommonResponse<List<Inspection>> searchInspectionTemplate(@RequestParam(required = false, defaultValue = "") String inspectionName, @RequestParam(required = false, defaultValue = "0") Integer inspectionUserId,
                                                                     @RequestParam(required = false, defaultValue = "0") Integer inspectionClassId){
        List<Inspection> inspectionList = inspectionService.searchInspectionTemplate(inspectionName, inspectionUserId, inspectionClassId);
        if(inspectionList.isEmpty() || inspectionList == null){
            logger.error("任务模板搜索失败");
            return CommonResponse.createForError("任务模板搜索失败");
        }
        logger.info("任务模板搜索成功");
        return CommonResponse.createForSuccess("SUCCESS", inspectionList);
    }

    /**
     * 数据组搜索
     */
    @GetMapping("/data/search")
    public CommonResponse<List<InspectionGroup>> searchInspectionGroup(@RequestParam String inspectionGroupName){
        List<InspectionGroup> inspectionGroupList = new ArrayList<>();
        InspectionGroup inspectionGroup = inspectionGroupService.getInspectionGroupByGroupName(inspectionGroupName);
        if(inspectionGroup == null){
            logger.error("数据组搜索失败");
            return CommonResponse.createForError("数据组搜索失败");
        }
        logger.info("数据组搜索成功");
        inspectionGroupList.add(inspectionGroup);
        return CommonResponse.createForSuccess("SUCCESS", inspectionGroupList);
    }
}
